package com.hnhegui.backend.order.entity;

import com.hnhegui.backend.order.enu.DeliveryTypeEnum;
import com.hnhegui.backend.order.enu.OrderStatusEnum;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Document(indexName = "ecommerce_orders")
public class OrderDocument {
    @Id
    private String orderId;

    @Field(type = FieldType.Keyword)
    private String orderNo;

    @Field(type = FieldType.Keyword)
    private String userId;

    @Field(type = FieldType.Keyword)
    private OrderStatusEnum status;

    @Field(type = FieldType.Double)
    private BigDecimal totalAmount;

    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    private LocalDateTime createTime;

    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    private LocalDateTime updateTime;

    @Field(type = FieldType.Nested)
    private List<OrderItem> items;

    @Field(type = FieldType.Keyword)
    private DeliveryTypeEnum deliveryType;

    @Field(type = FieldType.Object)
    private ShippingAddress shippingAddress;

    @Field(type = FieldType.Object)
    private PaymentInfo paymentInfo;

    @Field(type = FieldType.Object)
    private Fulfillment fulfillment;

    /**
     * 订单商品项
     */
    @Data
    public static class OrderItem {
        @Field(type = FieldType.Keyword)
        private String productId;

        @Field(type = FieldType.Text)
        private String productName;

        @Field(type = FieldType.Integer)
        private Integer quantity;

        @Field(type = FieldType.Double)
        private BigDecimal price;

        @Field(type = FieldType.Double)
        private BigDecimal subtotal;
    }

    /**
     * 收货地址
     */
    @Data
    public static class ShippingAddress {
        @Field(type = FieldType.Keyword)
        private String province;

        @Field(type = FieldType.Keyword)
        private String city;

        @Field(type = FieldType.Keyword)
        private String district;

        @Field(type = FieldType.Text)
        private String detail;

        @Field(type = FieldType.Keyword)
        private String receiverName;

        @Field(type = FieldType.Keyword)
        private String receiverPhone;
    }

    /**
     * 支付信息
     */
    @Data
    public static class PaymentInfo {
        @Field(type = FieldType.Keyword)
        private String paymentId;

        @Field(type = FieldType.Keyword)
        private String paymentMethod;

        @Field(type = FieldType.Keyword)
        private String paymentStatus;

        @Field(type = FieldType.Keyword)
        private String transactionId;

        @Field(type = FieldType.Double)
        private BigDecimal paidAmount;

        @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
        private LocalDateTime paidTime;
    }

    /**
     * 履约信息
     */
    @Data
    public static class Fulfillment {
        @Field(type = FieldType.Keyword)
        private String fulfillmentType;

        @Field(type = FieldType.Keyword)
        private String fulfillmentStatus;

        @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
        private LocalDateTime fulfillmentTime;

        @Field(type = FieldType.Object)
        private ExpressInfo expressInfo;

        @Field(type = FieldType.Object)
        private PickupInfo pickupInfo;

        @Field(type = FieldType.Object)
        private VirtualInfo virtualInfo;
    }

    /**
     * 快递信息
     */
    @Data
    public static class ExpressInfo {
        @Field(type = FieldType.Keyword)
        private String expressCompany;

        @Field(type = FieldType.Keyword)
        private String trackingNo;
    }

    /**
     * 自提信息
     */
    @Data
    public static class PickupInfo {
        @Field(type = FieldType.Keyword)
        private String storeId;

        @Field(type = FieldType.Keyword)
        private String pickupCode;

        @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
        private LocalDateTime pickedUpTime;
    }

    /**
     * 虚拟商品信息
     */
    @Data
    public static class VirtualInfo {
        @Field(type = FieldType.Keyword)
        private String activationCode;

        @Field(type = FieldType.Keyword)
        private String downloadUrl;
    }
}
